<?php
/**
 * Created by PhpStorm.
 * User: zzw
 * Date: 17-3-31
 * Time: 下午2:00
 */
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Company extends Modules_Controller
{

    function __construct()
    {
        parent::__construct();

        $this->rules = array(
            "rule" => array(
                array(
                    "field" => "title",
                    "label" => lang('title'),
                    "rules" => "trim|required|min_length[1]"
                )
            ,array(
                    "field" => "timeline",
                    "label" => lang('time'),
                    "rules" => "trim|strtotime"
                )
            ,array(
                    "field" => "content",
                    "label" => lang('conent'),
                    "rules" => "trim"
                    // link_create tag 生成
                )
            ,array(
                    "field" => "photo",
                    "label" => lang('photo'),
                    "rules" => "trim"
                )
            )
        );

    }
    public function status($key) {

        $data = $this->input->get();
        $res = $this->model->set($key,array('status'=>1));
        if($res) {
            redirect(base64_decode($data['ref']));
        }
    }
    public function _edit_data()
    {
        $form=$this->input->post();
        $form['timeline'] = time();
        return $form;
    }
    public function copypro()
    {
        $ids = $this->input->post('ids');

        $rs=$this->model->get_one($ids);

        unset($rs['id']);
        unset($rs['sort_id']);
        unset($rs['timeline']);

        $id = $this->model->create($rs);
        if ($id) {
            $vdata['msg'] = '复制成功，请刷新查看';
            $vdata['status'] = 1;
        }else{
            $vdata['msg'] = '复制失败，请刷新后重试';
            $vdata['status'] = 0;
        }
        $this->output->set_content_type('application/json')->set_output(json_encode($vdata));
    }
    /**
     * 列表页面
     * @param  int $cid  栏目ID
     * @param  integer $page 分页ID
     * @return html view 输出
     */
    public function index($cid=false,$page=1)
    {
        // 栏目路径
        $vdata['cpath']= $this->mcol->get_path_more($this->cid);
        $vdata['cchildren'] = $this->mcol->get_cols($this->cid);
        $title = $this->mcol->get_one($this->cid,"title");
        $vdata['title'] = $title['title'];

        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

        $order = $this->_index_orders();
        if ($this->input->get('order',TRUE)) {
            // TODO: order
            // $orders = explode("-",$this->input->get('order',TRUE));
            $order = $this->input->get('order',TRUE);
        }
        $where_in = array("cid"=>$this->cid,"ccid"=>$this->ccid);
        // 条件必须
        $where = array_merge($this->_index_where(),$where_in);
        $vdata['pages'] = $this->_pages(site_url($this->class.'/index/'.$this->cid.'/'),$limit,$where,4);
        $vdata['list'] = $this->model->get_list($limit,$limit*($page-1),$order,$where);
        $this->load->model('category_model', 'mcategory');
        $industry = $this->mcategory->get_all(array('ctype'=>24,'audit'=>1),'id,title');
        $vdata['industry'] = $industry;
        $total = $this->model->get_status_nums();
        $vdata['total'] = $total['total'];
        $this->_display($vdata);
    }
    protected function _index_where(){
        $arr =array();
        if (isset($_GET['ctype']) && $_GET['ctype']) {
            $arr['ctype'] = $_GET['ctype'];
        }
        if (isset($_GET['name']) && $_GET['name']) {
            $arr['like 1'] = array('name',$_GET['name']);
        }
        if (isset($_GET['industry']) && $_GET['industry']) {
            $arr['like 2'] = array('industry',$_GET['industry']);
        }
        if (isset($_GET['is_combing']) && $_GET['is_combing'] && $_GET['is_combing'] != 3) {
            if($_GET['is_combing'] == 2) {
                $_GET['is_combing'] = 0;
            }
            $arr['is_combing'] = $_GET['is_combing'];
        }
        if (isset($_GET['status']) && $_GET['status'] && $_GET['status'] != 3) {
            if($_GET['status'] == 2) {
                $_GET['status'] = 0;
            }
            $arr['status'] = $_GET['status'];
        }
        //var_dump($arr);die();
        return $arr;
    }

}